package com.konne.nightmare.FastPublicOpinion.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.konne.nightmare.FastPublicOpinion.R;

/**
 * @Author: chen
 * @UpdateDate: 1/22/22 10:32 AM
 * @UpdateRemark: 更新说明
 */
public class ConfirmDialog extends Dialog implements View.OnClickListener {
    private static final String TAG = ConfirmDialog.class.getSimpleName();

    private TextView mOkButton;
    private TextView mCancelButton;
    private TextView tvTitle;
    private TextView tvContent;
    private EditText etContent;
    private ImageView imgContent;
    private Context context;

    private boolean mCanCancelOnTouchOutside = true;

    public ConfirmDialog(final Context context) {
        super(context, R.style.dialog_center_theme);
        setContentView(R.layout.dialog_confirm);
        this.context = context;
        final View contentView = findViewById(R.id.dialogContentView);

        getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    final RectF frame = new RectF(
                            contentView.getX(),
                            contentView.getY(),
                            contentView.getX() + contentView.getWidth(),
                            contentView.getY() + contentView.getHeight());
                    if (mCanCancelOnTouchOutside && !frame.contains(event.getX(), event.getY())) {
                        cancel();
                    }
                }
                return false;
            }
        });

        mOkButton = (TextView) findViewById(R.id.ok);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvContent = (TextView) findViewById(R.id.tv_content);
        imgContent = (ImageView) findViewById(R.id.img_content);
        etContent = (EditText) findViewById(R.id.et_content);
        mOkButton.setOnClickListener(this);
        mCancelButton = (TextView) findViewById(R.id.cancel);
        mCancelButton.setOnClickListener(this);
    }

    public void setCanCancelOnTouchOutside(final boolean can) {
        this.mCanCancelOnTouchOutside = can;
    }

    public void setTitle(final String text) {
        tvTitle.setText(text);
        tvTitle.setVisibility(View.VISIBLE);
    }

    public void setMessageImage(int imageId) {
        imgContent.setImageResource(imageId);
        imgContent.setVisibility(View.VISIBLE);
    }

    public void setMessage(final int stringId) {
        tvContent.setText(stringId);
        tvContent.setVisibility(View.VISIBLE);
    }

    public void setMessage(final String text) {
        tvContent.setText(text);
        tvContent.setVisibility(View.VISIBLE);
    }

    public void setMessageColor(int colorId) {
        tvContent.setTextColor(getContext().getResources().getColor(colorId));
    }

    public void setOkButton(final String text, final View.OnClickListener l) {
        mOkButton.setText(text);
        if (l != null) {
            mOkButton.setOnClickListener(l);
        }
    }

    public void setCancelButton(String text, final View.OnClickListener l) {
        mCancelButton.setText(text);
        if (l != null) {
            mCancelButton.setOnClickListener(l);
        }
    }


    @Override
    public void onClick(View v) {
        cancel();
    }
}
